<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class LoginRequest extends FormRequest
{
    /**
     * 用户是否有权限请求
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 精确到控制器方法的验证
     *
     * @return array
     */
    public function rules()
    {
        $actionMethod = request()->route()->getActionMethod();
        $ruleMethod = $actionMethod . 'Rule';
        if (method_exists($this, $ruleMethod)) {
            return $this->{$ruleMethod}();
        }

        return [];
    }

    private function storeRule()
    {
        $rule = [
            #DummyRule
        ];

        return $rule;
    }

    private function updateRule()
    {
        $rule = [
            #DummyRule
        ];

        return $rule;
    }

    private function code2tokenRule()
    {
        $rule = [
            'code' => 'required|string',
            'platform' => [
                'required',
                'string',
                Rule::in(['wx', 'qq', 'tt'])
            ]
        ];

        return $rule;
    }

    private function code2tokenV2Rule()
    {
        $rule = [
            'code' => 'required|string',
            'platform' => [
                'required',
                'string',
                Rule::in(['wx', 'qq', 'tt'])
            ]
        ];

        return $rule;
    }

    public function loginRule()
    {
        $rule = [
            'captcha_key' => 'required|string|bail',
            'captcha_code' => 'required|captcha_api:' . request('captcha_key') . ',math|bail',
            'account' => 'required|string',
            'password' => 'required|string',
        ];

        return $rule;
    }

    public function registerRule()
    {
        $rule = [
            'captcha_key' => 'required|string|bail',
            'captcha_code' => 'required|captcha_api:' . request('captcha_key') . ',math|bail',
            'account' => 'required|string|unique:members,account|bail',
            'password' => 'required|string',
        ];

        return $rule;
    }

    public function attributes()
    {
        return [
            'account' => '用户名',
            'password' => '密码',
            'captcha_key' => '验证码',
            'captcha_code' => '验证码',
        ];
    }

    public function messages()
    {
        return [
            'captcha_key.required' => '验证码不能为空',
            'captcha_code.captcha_api' => '请输入正确的验证码',
        ];
    }
    // 常用验证方法 https://learnku.com/docs/laravel/5.8/validation/3899#rule-accepted
}
